import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 应用主题配置
/// 定义柔和疗愈的设计风格
class AppTheme {
  // 私有构造函数，防止实例化
  AppTheme._();

  // ==================== 颜色定义 ====================
  
  /// 主色调 - 现代渐变蓝紫色
  static const Color primaryColor = Color(0xFF667EEA);
  static const Color primaryLightColor = Color(0xFF764BA2);
  static const Color primaryDarkColor = Color(0xFF4C63D2);
  
  /// 次要色调 - 温暖的珊瑚色
  static const Color secondaryColor = Color(0xFFFF6B9D);
  static const Color secondaryLightColor = Color(0xFFFFB3D1);
  static const Color secondaryDarkColor = Color(0xFFE91E63);
  
  /// 背景色
  static const Color backgroundLight = Color(0xFFF5F7FA);
  static const Color backgroundDark = Color(0xFF1A1B23);
  
  /// 表面色
  static const Color surfaceLight = Color(0xFFFFFFFF);
  static const Color surfaceDark = Color(0xFF2D2E37);
  
  /// 卡片色
  static const Color cardLight = Color(0xFFFFFFFF);
  static const Color cardDark = Color(0xFF2D2E37);
  
  /// 文字颜色
  static const Color textPrimaryLight = Color(0xFF2C3E50);
  static const Color textPrimaryDark = Color(0xFFECF0F1);
  static const Color textSecondaryLight = Color(0xFF7F8C8D);
  static const Color textSecondaryDark = Color(0xFFBDC3C7);
  
  /// 分割线颜色
  static const Color dividerLight = Color(0xFFE8E8E8);
  static const Color dividerDark = Color(0xFF3A3B44);
  
  /// 错误色
  static const Color errorColor = Color(0xFFE74C3C);
  static const Color errorLightColor = Color(0xFFF8D7DA);
  
  /// 成功色
  static const Color successColor = Color(0xFF27AE60);
  static const Color successLightColor = Color(0xFFD4EDDA);
  
  /// 警告色
  static const Color warningColor = Color(0xFFF39C12);
  static const Color warningLightColor = Color(0xFFFFF3CD);
  
  /// 信息色
  static const Color infoColor = Color(0xFF3498DB);
  static const Color infoLightColor = Color(0xFFD1ECF1);
  
  /// 强调色
  static const Color accentColor = Color(0xFFE8A5C8);
  
  /// 情绪颜色映射
  static const Map<String, Color> moodColors = {
    'happy': Color(0xFFFFD93D),      // 快乐 - 明亮黄色
    'excited': Color(0xFFFF6B6B),    // 兴奋 - 活力红色
    'calm': Color(0xFF4ECDC4),       // 平静 - 薄荷绿
    'relaxed': Color(0xFF95E1D3),    // 放松 - 浅绿色
    'content': Color(0xFFB8E6B8),    // 满足 - 柔和绿
    'neutral': Color(0xFFBDC3C7),    // 中性 - 灰色
    'tired': Color(0xFF9B9B9B),      // 疲惫 - 深灰色
    'stressed': Color(0xFFFF8C42),   // 压力 - 橙色
    'anxious': Color(0xFFFFAB91),    // 焦虑 - 浅橙色
    'sad': Color(0xFF74B9FF),        // 悲伤 - 蓝色
    'angry': Color(0xFFE17055),      // 愤怒 - 红橙色
    'depressed': Color(0xFF636E72),  // 抑郁 - 深灰蓝
  };
  
  // ==================== 渐变色定义 ====================
  
  /// 主要渐变
  static const LinearGradient primaryGradient = LinearGradient(
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
    colors: [Color(0xFF667EEA), Color(0xFF764BA2)],
  );
  
  /// 次要渐变
  static const LinearGradient secondaryGradient = LinearGradient(
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
    colors: [secondaryLightColor, secondaryColor],
  );
  
  /// 背景渐变
  static const LinearGradient backgroundGradient = LinearGradient(
    begin: Alignment.topCenter,
    end: Alignment.bottomCenter,
    colors: [Color(0xFFF5F7FA), Color(0xFFE8EAF6)],
  );
  
  /// 卡片渐变背景
  static const LinearGradient cardBackgroundGradient = LinearGradient(
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
    colors: [Color(0xFFFFFFFF), Color(0xFFF8F9FA)],
  );
  
  /// 情绪卡片渐变
  static const LinearGradient moodCardGradient = LinearGradient(
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
    colors: [Color(0xFFFFFFFF), Color(0xFFF0F4F8)],
  );
  
  /// 卡片渐变
  static const LinearGradient cardGradient = LinearGradient(
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
    colors: [Color(0xFFFFFFFF), Color(0xFFF8FAFE)],
  );
  
  // ==================== 阴影定义 ====================
  
  /// 轻微阴影
  static const BoxShadow lightShadow = BoxShadow(
    color: Color(0x0D000000),
    offset: Offset(0, 2),
    blurRadius: 8,
    spreadRadius: 0,
  );
  
  /// 中等阴影
  static const BoxShadow mediumShadow = BoxShadow(
    color: Color(0x1A000000),
    offset: Offset(0, 4),
    blurRadius: 16,
    spreadRadius: 0,
  );
  
  /// 重阴影
  static const BoxShadow heavyShadow = BoxShadow(
    color: Color(0x26000000),
    offset: Offset(0, 8),
    blurRadius: 24,
    spreadRadius: 0,
  );
  
  /// 卡片阴影列表
  static const List<BoxShadow> cardShadow = [
    BoxShadow(
      color: Color(0x0D000000),
      offset: Offset(0, 2),
      blurRadius: 8,
      spreadRadius: 0,
    ),
  ];
  
  // ==================== 圆角定义 ====================
  
  /// 小圆角
  static const double radiusSmall = 8.0;
  
  /// 中等圆角
  static const double radiusMedium = 12.0;
  
  /// 大圆角
  static const double radiusLarge = 16.0;
  
  /// 超大圆角
  static const double radiusXLarge = 24.0;
  
  // ==================== 间距定义 ====================
  
  /// 超小间距
  static const double spacingXS = 4.0;
  
  /// 小间距
  static const double spacingS = 8.0;
  
  /// 中等间距
  static const double spacingM = 16.0;
  
  /// 大间距
  static const double spacingL = 24.0;
  
  /// 超大间距
  static const double spacingXL = 32.0;
  
  /// 超超大间距
  static const double spacingXXL = 48.0;
  
  // ==================== 字体大小定义 ====================
  
  /// 超小字体
  static const double fontSizeXS = 10.0;
  
  /// 小字体
  static const double fontSizeS = 12.0;
  
  /// 中等字体
  static const double fontSizeM = 14.0;
  
  /// 大字体
  static const double fontSizeL = 16.0;
  
  /// 超大字体
  static const double fontSizeXL = 18.0;
  
  /// 标题字体
  static const double fontSizeTitle = 20.0;
  
  /// 大标题字体
  static const double fontSizeLargeTitle = 24.0;
  
  /// 超大标题字体
  static const double fontSizeXLargeTitle = 28.0;
  
  // ==================== 颜色Getter方法 ====================
  
  /// 主要文字颜色
  static Color get textPrimaryColor => textPrimaryLight;
  
  /// 次要文字颜色
  static Color get textSecondaryColor => textSecondaryLight;
  
  /// 表面颜色
  static Color get surfaceColor => surfaceLight;
  
  /// 分割线颜色
  static Color get dividerColor => dividerLight;
  
  /// 背景颜色
  static Color get backgroundColor => backgroundLight;
  
  // ==================== 主题数据 ====================
  
  /// 浅色主题
  static ThemeData get lightTheme {
    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.light,
      
      // 颜色方案
      colorScheme: const ColorScheme.light(
        primary: primaryColor,
        primaryContainer: primaryLightColor,
        secondary: secondaryColor,
        secondaryContainer: secondaryLightColor,
        surface: surfaceLight,
        error: errorColor,
        onPrimary: Colors.white,
        onSecondary: Colors.white,
        onSurface: textPrimaryLight,
        onError: Colors.white,
      ),
      
      // 应用栏主题
      appBarTheme: const AppBarTheme(
        backgroundColor: Colors.transparent,
        elevation: 0,
        scrolledUnderElevation: 0,
        centerTitle: true,
        titleTextStyle: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeTitle,
          fontWeight: FontWeight.w600,
        ),
        iconTheme: IconThemeData(
          color: textPrimaryLight,
          size: 24,
        ),
        systemOverlayStyle: SystemUiOverlayStyle.dark,
      ),
      
      // 卡片主题
      cardTheme: CardThemeData(
        color: cardLight,
        elevation: 0,
        shadowColor: Colors.black.withValues(alpha: 0.1),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
        ),
      ),
      
      // 按钮主题
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: primaryColor,
          foregroundColor: Colors.white,
          elevation: 0,
          shadowColor: Colors.transparent,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radiusMedium),
          ),
          padding: const EdgeInsets.symmetric(
            horizontal: spacingL,
            vertical: spacingM,
          ),
          textStyle: const TextStyle(
            fontSize: fontSizeM,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
      
      // 文本按钮主题
      textButtonTheme: TextButtonThemeData(
        style: TextButton.styleFrom(
          foregroundColor: primaryColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radiusMedium),
          ),
          padding: const EdgeInsets.symmetric(
            horizontal: spacingM,
            vertical: spacingS,
          ),
          textStyle: const TextStyle(
            fontSize: fontSizeM,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      
      // 输入框主题
      inputDecorationTheme: InputDecorationTheme(
        filled: true,
        fillColor: surfaceLight,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: BorderSide(
            color: dividerLight,
            width: 1,
          ),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: BorderSide(
            color: dividerLight,
            width: 1,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: const BorderSide(
            color: primaryColor,
            width: 2,
          ),
        ),
        errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: const BorderSide(
            color: errorColor,
            width: 1,
          ),
        ),
        contentPadding: const EdgeInsets.symmetric(
          horizontal: spacingM,
          vertical: spacingM,
        ),
        hintStyle: const TextStyle(
          color: textSecondaryLight,
          fontSize: fontSizeM,
        ),
        labelStyle: const TextStyle(
          color: textSecondaryLight,
          fontSize: fontSizeM,
        ),
      ),
      
      // 分割线主题
      dividerTheme: const DividerThemeData(
        color: dividerLight,
        thickness: 1,
        space: 1,
      ),
      
      // 图标主题
      iconTheme: const IconThemeData(
        color: textSecondaryLight,
        size: 24,
      ),
      
      // 文本主题
      textTheme: const TextTheme(
        displayLarge: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeXLargeTitle,
          fontWeight: FontWeight.bold,
        ),
        displayMedium: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeLargeTitle,
          fontWeight: FontWeight.bold,
        ),
        displaySmall: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeTitle,
          fontWeight: FontWeight.w600,
        ),
        headlineLarge: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeXL,
          fontWeight: FontWeight.w600,
        ),
        headlineMedium: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeL,
          fontWeight: FontWeight.w600,
        ),
        headlineSmall: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeM,
          fontWeight: FontWeight.w600,
        ),
        bodyLarge: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeL,
          fontWeight: FontWeight.normal,
        ),
        bodyMedium: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeM,
          fontWeight: FontWeight.normal,
        ),
        bodySmall: TextStyle(
          color: textSecondaryLight,
          fontSize: fontSizeS,
          fontWeight: FontWeight.normal,
        ),
        labelLarge: TextStyle(
          color: textPrimaryLight,
          fontSize: fontSizeM,
          fontWeight: FontWeight.w500,
        ),
        labelMedium: TextStyle(
          color: textSecondaryLight,
          fontSize: fontSizeS,
          fontWeight: FontWeight.w500,
        ),
        labelSmall: TextStyle(
          color: textSecondaryLight,
          fontSize: fontSizeXS,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }
  
  /// 深色主题
  static ThemeData get darkTheme {
    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.dark,
      
      // 颜色方案
      colorScheme: const ColorScheme.dark(
        primary: primaryLightColor,
        primaryContainer: primaryDarkColor,
        secondary: secondaryLightColor,
        secondaryContainer: secondaryDarkColor,
        surface: surfaceDark,
        error: errorColor,
        onPrimary: Colors.black,
        onSecondary: Colors.black,
        onSurface: textPrimaryDark,
        onError: Colors.white,
      ),
      
      // 应用栏主题
      appBarTheme: const AppBarTheme(
        backgroundColor: Colors.transparent,
        elevation: 0,
        scrolledUnderElevation: 0,
        centerTitle: true,
        titleTextStyle: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeTitle,
          fontWeight: FontWeight.w600,
        ),
        iconTheme: IconThemeData(
          color: textPrimaryDark,
          size: 24,
        ),
        systemOverlayStyle: SystemUiOverlayStyle.light,
      ),
      
      // 卡片主题
      cardTheme: CardThemeData(
        color: cardDark,
        elevation: 0,
        shadowColor: Colors.black.withValues(alpha: 0.3),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
        ),
      ),
      
      // 按钮主题
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: primaryLightColor,
          foregroundColor: Colors.black,
          elevation: 0,
          shadowColor: Colors.transparent,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radiusMedium),
          ),
          padding: const EdgeInsets.symmetric(
            horizontal: spacingL,
            vertical: spacingM,
          ),
          textStyle: const TextStyle(
            fontSize: fontSizeM,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
      
      // 文本按钮主题
      textButtonTheme: TextButtonThemeData(
        style: TextButton.styleFrom(
          foregroundColor: primaryLightColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radiusMedium),
          ),
          padding: const EdgeInsets.symmetric(
            horizontal: spacingM,
            vertical: spacingS,
          ),
          textStyle: const TextStyle(
            fontSize: fontSizeM,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      
      // 输入框主题
      inputDecorationTheme: InputDecorationTheme(
        filled: true,
        fillColor: surfaceDark,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: BorderSide(
            color: dividerDark,
            width: 1,
          ),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: BorderSide(
            color: dividerDark,
            width: 1,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: const BorderSide(
            color: primaryLightColor,
            width: 2,
          ),
        ),
        errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(radiusMedium),
          borderSide: const BorderSide(
            color: errorColor,
            width: 1,
          ),
        ),
        contentPadding: const EdgeInsets.symmetric(
          horizontal: spacingM,
          vertical: spacingM,
        ),
        hintStyle: const TextStyle(
          color: textSecondaryDark,
          fontSize: fontSizeM,
        ),
        labelStyle: const TextStyle(
          color: textSecondaryDark,
          fontSize: fontSizeM,
        ),
      ),
      
      // 分割线主题
      dividerTheme: const DividerThemeData(
        color: dividerDark,
        thickness: 1,
        space: 1,
      ),
      
      // 图标主题
      iconTheme: const IconThemeData(
        color: textSecondaryDark,
        size: 24,
      ),
      
      // 文本主题
      textTheme: const TextTheme(
        displayLarge: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeXLargeTitle,
          fontWeight: FontWeight.bold,
        ),
        displayMedium: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeLargeTitle,
          fontWeight: FontWeight.bold,
        ),
        displaySmall: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeTitle,
          fontWeight: FontWeight.w600,
        ),
        headlineLarge: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeXL,
          fontWeight: FontWeight.w600,
        ),
        headlineMedium: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeL,
          fontWeight: FontWeight.w600,
        ),
        headlineSmall: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeM,
          fontWeight: FontWeight.w600,
        ),
        bodyLarge: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeL,
          fontWeight: FontWeight.normal,
        ),
        bodyMedium: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeM,
          fontWeight: FontWeight.normal,
        ),
        bodySmall: TextStyle(
          color: textSecondaryDark,
          fontSize: fontSizeS,
          fontWeight: FontWeight.normal,
        ),
        labelLarge: TextStyle(
          color: textPrimaryDark,
          fontSize: fontSizeM,
          fontWeight: FontWeight.w500,
        ),
        labelMedium: TextStyle(
          color: textSecondaryDark,
          fontSize: fontSizeS,
          fontWeight: FontWeight.w500,
        ),
        labelSmall: TextStyle(
          color: textSecondaryDark,
          fontSize: fontSizeXS,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }
  
  // ==================== 常用装饰 ====================
  
  /// 卡片装饰
  static BoxDecoration get cardDecoration => BoxDecoration(
    color: surfaceLight,
    borderRadius: BorderRadius.circular(radiusMedium),
    boxShadow: const [lightShadow],
  );
  
  /// 深色模式卡片装饰
  static BoxDecoration get cardDecorationDark => BoxDecoration(
    color: surfaceDark,
    borderRadius: BorderRadius.circular(radiusMedium),
    boxShadow: const [mediumShadow],
  );
  
  // ==================== 辅助方法 ====================
  
  /// 根据情绪类型获取对应颜色
  /// [moodType] 情绪类型
  /// 返回对应的颜色，如果未找到则返回中性色
  static Color getMoodColor(String moodType) {
    return moodColors[moodType.toLowerCase()] ?? moodColors['neutral']!;
  }
  
  /// 获取情绪颜色的浅色版本
  /// [moodType] 情绪类型
  /// [opacity] 透明度，默认0.2
  static Color getMoodColorLight(String moodType, {double opacity = 0.2}) {
    return getMoodColor(moodType).withValues(alpha: opacity);
  }
  
  /// 创建带阴影的装饰
  /// [color] 背景颜色
  /// [radius] 圆角半径
  /// [shadow] 阴影类型
  static BoxDecoration createShadowDecoration({
    Color? color,
    double? radius,
    BoxShadow? shadow,
  }) {
    return BoxDecoration(
      color: color ?? surfaceLight,
      borderRadius: BorderRadius.circular(radius ?? radiusMedium),
      boxShadow: [shadow ?? lightShadow],
    );
  }
  
  /// 创建渐变装饰
  /// [gradient] 渐变
  /// [radius] 圆角半径
  /// [shadow] 阴影
  static BoxDecoration createGradientDecoration({
    required Gradient gradient,
    double? radius,
    BoxShadow? shadow,
  }) {
    return BoxDecoration(
      gradient: gradient,
      borderRadius: BorderRadius.circular(radius ?? radiusMedium),
      boxShadow: shadow != null ? [shadow] : null,
    );
  }
  
  /// 创建边框装饰
  /// [color] 边框颜色
  /// [width] 边框宽度
  /// [radius] 圆角半径
  /// [backgroundColor] 背景颜色
  static BoxDecoration createBorderDecoration({
    Color? color,
    double? width,
    double? radius,
    Color? backgroundColor,
  }) {
    return BoxDecoration(
      color: backgroundColor,
      border: Border.all(
        color: color ?? dividerLight,
        width: width ?? 1,
      ),
      borderRadius: BorderRadius.circular(radius ?? radiusMedium),
    );
  }
}